package cx.zan.web.servlet;

import cx.zan.domain.User;
import cx.zan.service.UserService;
import cx.zan.utils.MyBeanUtil;
import cx.zan.utils.UUIDUtil;
import cx.zan.vo.ResultVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author Mxb
 * @version 1.0
 * @date 2020/3/30 14:40
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserService();

    //@Override
    //protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //    // // 都返回json
    //    //ResultVo vo =  new ResultVo("hello world");
    //    //
    //    // //对象转json
    //    // ObjectMapper mapper = new ObjectMapper();
    //    // String s = mapper.writeValueAsString(vo);
    //    //
    //    // response.getWriter().print(s);
    //    //获取md参数
    //    String md = request.getParameter("md");
    //    if (md == null || md.length() == 0) {
    //        response.getWriter().print("滚蛋");
    //        return;
    //    }
    //    //判断调用方法
    //    //if("regist".equals(md)){
    //    //    regist(request,response);
    //    //}else if("login".equals(md)){
    //    //    login(request,response);
    //    //}else if("logout".equals(md)){
    //    //    logout(request,response);
    //    //}
    //    //用反射
    //    try {
    //        Method method = this.getClass().getMethod(md, HttpServletRequest.class, HttpServletResponse.class);
    //        method.invoke(this, request, response);
    //
    //    } catch (NoSuchMethodException e) {
    //        e.getMessage();
    //    } catch (IllegalAccessException e) {
    //        e.printStackTrace();
    //    } catch (InvocationTargetException e) {
    //        e.printStackTrace();
    //        response.getWriter().print("滚蛋");
    //        return;
    //    }
    //}

    //@Override
    //protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //    super.doPost(request, response);
    //}

    /**
     * 注册功能
     * @param request
     * @param response
     * @throws IOException
     * @throws SQLException
     */
    public void regist(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
        //数据校验
        //集合保存错误信息
        //Map<String,String> errors = new HashMap<>();
        ////用户名 长度 2-8 密码必须2-8
        //String username = request.getParameter("username");
        //if(username==null || username.trim().length()<=2 || username.trim().length()>8){
        //    //不对
        //    errors.put("username","长度必须2-8之间");
        //}
        //String password = request.getParameter("password");
        //if(password==null || password.trim().length()<=2 || password.trim().length()>8){
        //    //不对
        //    errors.put("password","长度必须2-8之间");
        //}
        ////判断一下集合是否为空
        //if(errors.size()>0){
        //    //有错
        //    //把错误写出去
        //    writeFail(errors);
        //}

        //获取表单数据 单个
        ////String username = request.getParameter("username");
        ////获取整个表单数据
        Map<String, String[]> data = request.getParameterMap();
        //// 封装对象
        User user = new User();
        ////调用工具类 把集合
        MyBeanUtil.populate(user,data);
        System.out.println(data);
        //
        ////生成用户id
        String id = UUIDUtil.getId();
        user.setUid(id);
        //
        //// 验证数据
        //
        ////调用service
        int i = userService.saveUser(user);
        //if(i>=1){
        //    writeSuccess("注册成功");
        //
        //}else{
        //    writeFail("注册失败！");
        //
        //}
        //返回结果
        writeSuccess("注册成功");

        //response.getWriter().print("注册功能");

    }

    /**
     * 登录
     * @param request
     * @param response
     * @throws IOException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userService.login(username, password);
        if(user!=null){
            //不为空
            //把当前用户对象放入到 session中   用户问你是否登录 判断它的session中是否存在user对象
            HttpSession session = request.getSession();
            session.setAttribute("x-user",user);

            writeSuccess("登录成功");

        }else {
            writeFail("账号或密码错误");
        }
        //response.getWriter().print("登录功能");
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @throws IOException
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //如何销毁session
        request.getSession().invalidate();
        writeSuccess("退出登录成功");
        //response.getWriter().print("退出功能");
    }


    //获取当前登录人的信息
    public void myname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取你session中是否存在 user对象'
        User user = (User) request.getSession().getAttribute("x-user");
        if (user==null){
            writeJson(new ResultVo(ResultVo.CODE_NOLOGIN,"未登录状态"));
        }else{
            String nickName = user.getName();
            writeSuccess(nickName);
        }
    }

}
